﻿using BaseTypesLib.Interfaces;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Xml.Linq;
using System.Linq;

namespace BaseTypesLib.Model
{
    [DataContract(IsReference=true)]
    public class GaugeField:Field, IEditable, IReadOnly
    {
        private bool labelAsProgress, noText, percent;
        private int min, max, val;
        private string label;
        private bool readOnly, editable;

        [DataMember]
        public string Label
        {
            get { return label; }
            set
            {
                if (label != value)
                {
                    label = value;
                    OnPropertyChanged("Label");
                }
            }
        }
        [DataMember]
        public bool Percent
        {
            get { return percent; }
            set
            {
                if (percent != value)
                {
                    percent = value;
                    OnPropertyChanged("Percent");
                }
            }
        }
        [DataMember]
        public bool NoText
        {
            get { return noText; }
            set
            {
                if (noText != value)
                {
                    noText = value;
                    OnPropertyChanged("NoText");
                }
            }
        }
        [DataMember]
        public bool LabelAsProgress
        {
            get { return labelAsProgress; }
            set
            {
                if (labelAsProgress != value)
                {
                    labelAsProgress = value;
                    OnPropertyChanged("LabelAsProgress");
                }
            }
        }

        [DataMember]
        public int Minimum
        {
            get { return min; }
            set
            {
                if (min != value)
                {
                    min = value;
                    OnPropertyChanged("Minimum");
                    OnPropertyChanged("PercentValue");
                }
            }
        }
        [DataMember]
        public int Maximum
        {
            get { return max; }
            set
            {
                if (max != value)
                {
                    max = value;
                    OnPropertyChanged("Maximum");
                    OnPropertyChanged("PercentValue");
                }
            }
        }
        [DataMember]
        public int Value
        {
            get { return val; }
            set
            {
                if (val != value)
                {
                    val = value;
                    OnPropertyChanged("Value");
                    OnPropertyChanged("PercentValue");
                }
            }
        }
        [Browsable(false)]
        public int PercentValue
        {
            get
            {
                if (min == max) return 0;
                return (val - min) * 100 / (max - min);
            }

        }

        [DataMember]
        public bool ReadOnly
        {
            get { return readOnly; }
            set
            {
                if (readOnly != value)
                {
                    readOnly = value;
                    OnPropertyChanged("ReadOnly");
                }
            }
        }

        [DataMember]
        public bool Editable
        {
            get { return editable; }
            set
            {
                if (editable != value)
                {
                    editable = value;
                    OnPropertyChanged("Editable");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();
            if (Label != null && Label.Trim().Length > 0)
                el.SetAttributeValue("Label", Label.Trim());
            if (NoText)
                el.SetAttributeValue("NoText", NoText);
            if (LabelAsProgress)
                el.SetAttributeValue("LabelAsProgress", LabelAsProgress);
            if (Percent)
                el.SetAttributeValue("Percent", Percent);

            el.SetAttributeValue("Minimum", Minimum);
            el.SetAttributeValue("Maximum", Maximum);
            el.SetAttributeValue("Value", Value);

            if (ReadOnly)
                el.SetAttributeValue("ReadOnly", ReadOnly);
            if (!Editable)
                el.SetAttributeValue("Editable", Editable);

            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var lbl = elem.Attributes("Label").FirstOrDefault();
            if (lbl != null)
                Label = lbl.Value;

            var min = elem.Attributes("Minimum").FirstOrDefault();
            if (min != null) Minimum = (int)min;
            var max = elem.Attributes("Maximum").FirstOrDefault();
            if (max != null) Maximum = (int)max;
            var val = elem.Attributes("Value").FirstOrDefault();
            if (val != null) Value = (int)val;

            var per = elem.Attributes("Percent").FirstOrDefault();
            if (per != null) Percent = (bool)per;
            var nt = elem.Attributes("NoText").FirstOrDefault();
            if (nt != null) NoText = (bool)nt;
            var lap = elem.Attributes("LabelAsProgress").FirstOrDefault();
            if (lap != null) LabelAsProgress = (bool)lap;


            var ro = elem.Attributes("ReadOnly").FirstOrDefault();
            if (ro != null) ReadOnly = (bool)ro;
            var e = elem.Attributes("Editable").FirstOrDefault();
            if (e != null) Editable = (bool)e;
        }
    }
}
